﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.ComponentModel.DataAnnotations;
using MobileSales.Model.Localization;

namespace MobileSales.Model.Params
{
    [DataContract]
    public abstract class ParamObject
    {
        #region Protected members
        protected Dictionary<Param, string> _params; 
        #endregion

        #region Public members
        [DataMember]
        [Display(Name = "Parameters", ResourceType = typeof(ModelStrings))]
        public Dictionary<Param, string> Params
        {
            get { return _params; }
            set { _params = value; }
        } 
        #endregion

        #region Constructor
        public ParamObject()
        {
            _params = new Dictionary<Param, string>();
        }      
        #endregion   
        
        #region Indexer
        public object this[string paramName]
        {
            get
            {
                Param param = this.GetParamByName(paramName);
                if (param != null)
                {
                    return (param.FieldType == FieldType.Dictionary) 
                        ? _params[param]
                        : param.GetValue(_params[param]);
                }
                return null;
            }

            set
            {
                Param param = this.GetParamByName(paramName);
                if (param !=null) 
                {
                    _params[param] = value.ToString();
                }
            }
        }

        #endregion

        #region GetParam
        public T GetParam<T>(string paramName)
        {
            return (T)this[paramName];
        } 
        #endregion

        #region GetParamByName
        protected Param GetParamByName(string paramName)
        {
            if (_params != null)
            {
                return _params.Keys.Where(x => x.Name.Equals(paramName)).FirstOrDefault();
            }
            return null;
        } 
        #endregion
    }

}
